package com.tb.controller;

import com.github.pagehelper.PageInfo;
import com.tb.model.*;
import com.tb.service.*;
import com.tb.util.MyConstant;
import com.tb.util.MyUtils;
import com.tb.util.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @description: 提问问题相关
 * @author: wangguichun
 * @create: 2018-04-14 13:41
 **/

@Controller
@RequestMapping("ques")
public class QuestionController {

    @Autowired
    private QuestionService questionService;
    @Autowired
    private TopicService topicService;
    @Autowired
    private AnswerService answerService;
    @Autowired
    private StarService starService;
    @Autowired
    private FollowService followService;


    @RequestMapping("ask")
    @ResponseBody
    public Response ask(Question question) {
        int questionId = questionService.addQuestion(question);
        return new Response(0, "", questionId);
    }


    //按板块分类的提问
    @RequestMapping("list/{topicId}/{pageId}")
    public String list(@PathVariable Integer topicId, @PathVariable Integer pageId, Model model) {
        Topic topic = topicService.getTopicById(topicId);
        PageInfo<Question> pageInfo = questionService.getQuestionByTopicIdAndPageID(topicId, pageId);
        model.addAttribute("topic", topic);
//        model.addAttribute("pageBean", pageBean);
        model.addAttribute("pageInfo", pageInfo);
        return "question/list";
    }


    //热门提问
    @RequestMapping("hot/{pageId}")
    public String hotList(@PathVariable Integer pageId, Model model) {
        PageInfo<Question> pageInfo = questionService.getHotQuestionByPageID(pageId);
        Topic topic = new Topic();
        topic.setName("热门话题");
        topic.setImage(MyConstant.TOPIC_HOT_URL);
        model.addAttribute("topic", topic);
//        model.addAttribute("pageBean", pageBean);
        model.addAttribute("pageInfo", pageInfo);
        return "question/list";
    }

    //最新提问
    @RequestMapping("lately/{pageId}")
    public String latelyList(@PathVariable Integer pageId, Model model) {
        PageInfo<Question> pageInfo = questionService.getLatelyQuestionByPageID(pageId);
        Topic topic = new Topic();
        topic.setName("最新话题");
        topic.setImage(MyConstant.TOPIC_NEW_URL);
        model.addAttribute("topic", topic);
//        model.addAttribute("pageBean", pageBean);
        model.addAttribute("pageInfo", pageInfo);
        return "question/list";
    }

    //我的收藏
    @RequestMapping("myStar/{pageId}")
    public String myStarList(@PathVariable Integer pageId, Model model, HttpServletRequest request) {
        PageInfo<Question> pageInfo = questionService.getMyStarQuestionByPageID(pageId, request);
        Topic topic = new Topic();
        topic.setName("我的收藏");
        topic.setImage(MyConstant.TOPIC_STAR_URL);
        model.addAttribute("topic", topic);
//        model.addAttribute("pageBean", pageBean);
        model.addAttribute("pageInfo", pageInfo);
        return "question/list";
    }

    //搜索
    @RequestMapping("search/{pageId}")
    public String search(@PathVariable Integer pageId, String keyWord, Model model) {
        PageInfo<Question> pageInfo = questionService.getStarQuestionByKeyWordAndPageID(keyWord, pageId);
        Topic topic = new Topic();
        topic.setName("与 " + keyWord + " 相关");
        topic.setImage(MyConstant.TOPIC_SEARCH_URL);
        model.addAttribute("topic", topic);
//        model.addAttribute("pageBean", pageBean);
        model.addAttribute("pageInfo", pageInfo);
        return "question/list";
    }


    //我的提问
    @RequestMapping("myQues/{pageId}")
    public String myQuesList(@PathVariable Integer pageId, Model model, HttpServletRequest request) {
        PageInfo<Question> pageInfo = questionService.getMyQuestionByPageID(pageId, request);
        Topic topic = new Topic();
        topic.setName("我的提问");
        topic.setImage(MyConstant.TOPIC_MY_URL);
        model.addAttribute("topic", topic);
//        model.addAttribute("pageBean", pageBean);
        model.addAttribute("pageInfo", pageInfo);
        return "question/list";
    }

    //问题详情页
    @RequestMapping("{questionId}")
    public String question(@PathVariable Integer questionId, Model model, HttpServletRequest request) {
        //获取问题
        Question question = questionService.getQuestionWithCommentsById(questionId);
        //获取解答
        List<Answer> answerList = answerService.getAnswerWithCommentByQuestionId(questionId);
        //获取收藏的用户
        List<User> staredUsers = starService.getUserListByQuestionId(questionId);
        //检测登陆者是否为关注用户
        int userId = Integer.parseInt(MyUtils.getCookie(request, "id").getValue());
        boolean staredStatus = false;
        for (User staredUser : staredUsers) {
            if (staredUser.getId() == userId) {
                staredStatus = true;
                break;
            }
        }
        boolean followedStatus = followService.isFollow(userId,question.getUserId());
        model.addAttribute("question", question);
        model.addAttribute("answerList", answerList);
        model.addAttribute("staredUsers", staredUsers);
        model.addAttribute("staredStatus", staredStatus);
        model.addAttribute("followedStatus", followedStatus);

        return "question/detail";
    }

}
